package com.medvalley.tcm.backend.service;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GeTuiPushServiceImpl implements PushService {

    private static Logger LOG = LoggerFactory.getLogger(GeTuiPushServiceImpl.class);

    @Value("${getui.appId}")
    private String appId;
    @Value("${getui.appKey}")
    private String appKey;
    @Value("${getui.masterSecret}")
    private String masterSecret;

    private static String GE_TUI_SERVER_URL = "http://sdk.open.api.igexin.com/apiex.htm";


    @Override
    public void pushNewsLink(String newsTitle, String newsUrl) {
        IGtPush push = new IGtPush(GE_TUI_SERVER_URL, appKey, masterSecret);
        LinkTemplate template = new LinkTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTitle(newsTitle);
        template.setText(newsTitle);
        template.setUrl(newsUrl);
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);
        IPushResult ret = push.pushMessageToApp(message);
        LOG.info(ret.getResponse().toString());
    }
}
